﻿
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils

Partial Public Class device_manager
#If IT_IS_EVIL Then
    c, 14-12-23 12:03:54, caught unhandled exception, type System.TypeLoadException, source osi.service.device, message A generic instantiation for type 'osi.service.device.singleton_d
evice_pool`1' from assembly 'osi.service.device, Version=1.0.5469.25807, Culture=neutral, PublicKeyToken=null' was too deep.  Is this generic type defined or used recursively?, sta
cktrace    at osi.service.device.device_manager.register[T](String key, T d, Func`2 validator, Action`1 closer, Func`2 device_id) in C:\Users\Hzj_jie\Documents\Visual Studio 2013\P
rojects\gemini\osi\service\device\manager\device_manager.shortcut.vb:line 12
    Public Shared Function register(Of T)(ByVal key As String,
                                          ByVal d As T,
                                          Optional ByVal validator As Func(Of T, Boolean) = Nothing,
                                          Optional ByVal closer As Action(Of T) = Nothing,
                                          Optional ByVal device_id As Func(Of T, String) = Nothing) As Boolean
        Return Not d Is Nothing AndAlso
               register(key, New singleton_device_pool(Of T)(d, validator, closer, device_id))
    End Function
#End If

    Public Shared Function expired(ByVal key As String) As Boolean
        Dim o As idevice_pool = Nothing
        Return [get](key, o) AndAlso
               o.expired()
    End Function

    Public Shared Function total_count(ByVal key As String) As Int32
        Dim o As idevice_pool = Nothing
        If [get](key, o) Then
            Return o.total_count()
        Else
            Return 0
        End If
    End Function

    Public Shared Function free_count(ByVal key As String) As Int32
        Dim o As idevice_pool = Nothing
        If [get](key, o) Then
            Return o.free_count()
        Else
            Return 0
        End If
    End Function

    Public Shared Function sync_get(Of T, PT As isync_device_pool(Of T)) _
                                   (ByVal key As String,
                                    ByRef o As T,
                                    Optional ByRef p As PT = Nothing) As Boolean
        Return [get](key, p) AndAlso
                p.sync_get(o)
    End Function

    Public Shared Function sync_get(Of T)(ByVal key As String,
                                          ByRef o As T,
                                          Optional ByRef p As idevice_pool = Nothing) As Boolean
        Dim pp As isync_device_pool(Of T) = Nothing
        If [get](key, pp) Then
            p = pp
            Return pp.sync_get(o)
        Else
            Return False
        End If
    End Function

    Public Shared Function sync_get(Of T)(ByVal key As String) As T
        Dim o As T = Nothing
        assert(sync_get(Of T)(key, o))
        Return o
    End Function

    Public Shared Function sync_get(Of T, PT As isync_device_pool(Of T)) _
                                   (ByVal key As String,
                                    ByRef p As PT) As T
        Dim o As T = Nothing
        assert(sync_get(key, o, p))
        Return o
    End Function

    Public Shared Function sync_get(Of T)(ByVal key As String, ByRef p As idevice_pool) As T
        Dim o As T = Nothing
        assert(sync_get(key, o, p))
        Return o
    End Function

    Public Shared Function release(Of T)(ByVal key As String, ByVal c As T) As Boolean
        Dim p As idevice_pool(Of T) = Nothing
        Return [get](key, p) AndAlso
               p.release(c)
    End Function

    Public Shared Function [get](Of T, PT As idevice_pool(Of T)) _
                                (ByVal key As String,
                                 ByVal o As pointer(Of T),
                                 ByVal timeout_ms As Int64,
                                 ByVal p As pointer(Of PT)) As event_comb
        Dim ec As event_comb = Nothing
        Dim d As idevice_pool(Of T) = Nothing
        Return New event_comb(Function() As Boolean
                                  If [get](key, d) Then
                                      ec = d.get(o, timeout_ms)
                                      Return waitfor(ec) AndAlso
                                             goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         eva(p, d) AndAlso
                                         goto_end()
                              End Function)
    End Function

    Public Shared Function [get](Of T)(ByVal key As String,
                                       ByVal o As pointer(Of T),
                                       ByVal timeout_ms As Int64,
                                       ByVal p As pointer(Of idevice_pool)) As event_comb
        Dim ec As event_comb = Nothing
        Dim pp As pointer(Of idevice_pool(Of T)) = Nothing
        Return New event_comb(Function() As Boolean
                                  pp = New pointer(Of idevice_pool(Of T))()
                                  ec = [get](key, o, timeout_ms, pp)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         eva(p, +pp) AndAlso
                                         goto_end()
                              End Function)
    End Function

    Public Shared Function [get](Of T)(ByVal key As String,
                                       ByVal o As pointer(Of T),
                                       ByVal timeout_ms As Int64) As event_comb
        Return [get](Of T)(key, o, timeout_ms, Nothing)
    End Function
End Class
